#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

//01 String深拷贝阶段
class String{
public:
    String()
        :_pstr(new char[1]()){

        }

    String(const char * pstr)
        :_pstr(new char[strlen(pstr)+1]()){
            strcpy(_pstr,pstr);
        }

    String(const String & rhs)
        :_pstr(new char[strlen(rhs._pstr)+1]()){
            strcpy(_pstr,rhs._pstr);
        }

    ~String(){
        if(_pstr){
            delete [] _pstr;
            _pstr = nullptr;
        }
    }

    size_t size() const{
        return strlen(_pstr);
    }

    const char * c_str() const{
        return _pstr;
    }

    String  operator=(const String & rhs){
        if(this != &rhs){
            delete [] _pstr;
            _pstr = new char[rhs.size()+1]();
            strcpy(_pstr,rhs._pstr);
        }
        return *this;
    }

    String & operator+=(const String & rhs){
        if(this != &rhs){
            char * temp = new char[size()+rhs.size()+1]();
            strcpy(temp,_pstr);
            strcat(temp,rhs._pstr);
            delete [] _pstr;
            _pstr = temp;
        }
        return *this;
    }

    String & operator+=(const char * pstr){
        String temp(pstr);
        *this += temp;
        return *this;
    }

    char & operator[](size_t index){
        if(index >= size()){
            static char nullchar = '\0';
            return nullchar;
        }
        return _pstr[index];
    }

    //第一个const：防止被修改值
    //第二个const：用于const对象的调用，不加const对象无法调用
    const char & operator[](size_t index) const {
        if(index >= size()){
            static char nullchar = '\0';
            return nullchar;
        }
        return _pstr[index];
    }

    friend bool operator==(const String & lhs,const String & rhs);
    friend bool operator!=(const String & lhs,const String & rhs);
    friend bool operator>=(const String & lhs,const String & rhs);
    friend bool operator<=(const String & lhs,const String & rhs);
    friend bool operator>(const String & lhs,const String & rhs);
    friend bool operator<(const String & lhs,const String & rhs);

    friend ostream & operator<<(ostream & os,const String & rhs);
    friend istream & operator>>(istream & is,String & rhs);
private:
    char * _pstr;
};

bool operator==(const String & lhs,const String & rhs){
    return strcmp(lhs._pstr,rhs._pstr)==0;
}

bool operator!= (const String & lhs,const String & rhs){
    return strcmp(lhs._pstr,rhs._pstr)!=0;
}

bool operator>= (const String & lhs,const String & rhs){
    return strcmp(lhs._pstr,rhs._pstr)>=0;
}

bool operator<= (const String & lhs,const String & rhs){
    return strcmp(lhs._pstr,rhs._pstr)<=0;
}

bool operator> (const String & lhs,const String & rhs){
    return strcmp(lhs._pstr,rhs._pstr)>0;
}

bool operator< (const String & lhs,const String & rhs){
    return strcmp(lhs._pstr,rhs._pstr)<0;
}

ostream & operator<<(ostream & os,const String & rhs){
    os << rhs._pstr;
    return os;
}

istream & operator>>(istream & is,String & rhs){
    char ch;
    vector<char> vec;
    while((ch = is.get()) != '\n'){
        vec.push_back(ch);
    }

    delete  [] rhs._pstr;
    rhs._pstr = new char[vec.size()+1];
    for(int i = 0; i < vec.size(); ++i){
        rhs. _pstr[i] = vec[i];
    }
    return is;
}

String operator+(const String & lhs,const String & rhs){
    String temp(lhs);
    temp += rhs;
    return temp;
}

String operator+(const String & lhs,const char * pstr){
    String temp(lhs);
    temp += pstr;
    return temp;
}


String operator+(const char * pstr,const String & rhs){
    String temp(pstr);
    temp += rhs;
    return temp;
}

void test(){
    String s1;
    cin >> s1;
    cout << s1 << endl;

    String s2("hello");
    cout << s2 << endl;
}

void test2(){
    String s1("hello");
    String s2("world");
    cout << s1 << endl;
    cout << s1.size() << endl;
    cout << s2 << endl;

    String s3(s1);
    cout << s3 << endl;
    s2 = s3;
    cout << s2 << endl;

    const char * pstr = s2.c_str();
    cout << pstr << endl;
}

void test3(){
    String s1("hello ");
    String s2("world");
    s1 += s2;
    cout << s1 << endl;

    const char * pstr = "java";
    s1 += pstr;
    cout << s1 << endl;
}

void test4(){
    String s1("hello");
    cout << s1 << endl;
    cout << s1[0] << endl;
    s1[0] = 'H';
    cout << s1 << endl;

    const String s2("world");
    cout << s2 << endl;
    cout << s2[0] << endl;
    //s2[0] = 'W'; error
}

void test5(){
    String s1("hello");
    String s2("hello");
    String s3("world");
    cout << (s1 == s2) << endl;
    cout << (s1 != s3) << endl;

    cout << (s1 >= s3) << endl;
    cout << (s1 > s2) << endl;
    cout << (s1 <= s3) << endl;
    cout << (s1 < s3) << endl;
}

int main()
{
    test5();
    return 0;
}

